using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
    public static partial class AdminService
    {
        public static bool AddAdmin(Admin admin)
        {
            string sql =
                "INSERT Admin (AdminName, PassWord, AdminDesc)" +
                "VALUES (@AdminName, @PassWord, @AdminDesc)";

            sql += " ; SELECT @@IDENTITY";


            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminName", admin.AdminName),
				new SqlParameter("@PassWord", admin.PassWord),
                new SqlParameter("@AdminDesc", admin.AdminDesc)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        public static bool DeleteAdminByAdminID(int adminID)
        {
            string sql = "DELETE Admin WHERE AdminID = @AdminID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminID", adminID)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public static bool ModifyAdmin(Admin admin)
        {
            string sql =
                "UPDATE Admin " +
                "SET " +
                    "AdminName = @AdminName, " +
                    "PassWord = @PassWord, " +
                    "AdminDesc = @AdminDesc " +
                "WHERE AdminID = @AdminID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminID", admin.AdminID),
				new SqlParameter("@AdminName", admin.AdminName),
				new SqlParameter("@PassWord", admin.PassWord),
                new SqlParameter("@AdminDesc", admin.AdminDesc)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static IList<Admin> GetAllAdmins()
        {
            string sqlAll = "SELECT * FROM Admin";
            return GetAdminsBySql(sqlAll);
        }

        public static IList<Admin> GetAllAdminsSort(string sortName, string sortType)
        {
            string sqlAll = "SELECT * FROM Admin order by " + sortName + " " + sortType;
            return GetAdminsBySql(sqlAll);
        }

        public static IList<Admin> GetAdminByName(string adminName)
        {
            string sqlAll = "SELECT * FROM Admin where AdminName=@AdminName";
            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminName", adminName)
			};
            return GetAdminsBySql(sqlAll, para);
        }

        public static Admin LoginByAdminName(string adminName)
        {
            string sql = "SELECT AdminID,PassWord FROM Admin WHERE AdminName = @AdminName";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminName", adminName)
			};

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);

            Admin admin = new Admin();

            foreach (DataRow dr in temp.Rows)
            {
                admin.AdminID = (int)dr["AdminID"];
                admin.PassWord = (string)dr["PassWord"];
            }
            return admin;

        }

        public static Admin GetAdminBytop1()
        {
            string sql = "SELECT top 1 * FROM Admin order by AdminID desc";

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, null);

            Admin admin = new Admin();

            foreach (DataRow dr in temp.Rows)
            {
                admin.AdminID = (int)dr["AdminID"];
                admin.AdminName = (string)dr["AdminName"];
                admin.PassWord = (string)dr["PassWord"];
                admin.AdminDesc = (string)dr["AdminDesc"];
            }
            return admin;

        }


        public static Admin GetAdminByAdminID(int adminID)
        {
            string sql = "SELECT * FROM Admin WHERE AdminID = @AdminID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminID", adminID)
			};

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);

            Admin admin = new Admin();

            foreach (DataRow dr in temp.Rows)
            {
                admin.AdminID = (int)dr["AdminID"];
                admin.AdminName = (string)dr["AdminName"];
                admin.PassWord = (string)dr["PassWord"];
                admin.AdminDesc = (string)dr["AdminDesc"];

            }
            return admin;

        }

        public static bool GetAdminByAdminIDandRow(int adminID, string title)
        {
            string sql = "SELECT " + title + " FROM Admin WHERE AdminID = @AdminID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminID", adminID)
			};

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);

            bool req = false;

            foreach (DataRow dr in temp.Rows)
            {
                req = (bool)dr[title];
            }
            return req;
        }


        private static IList<Admin> GetAdminsBySql(string safeSql)
        {
            List<Admin> list = new List<Admin>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
            {
                while (dr.Read())
                {
                    Admin admin = new Admin();

                    admin.AdminID = (int)dr["AdminID"];
                    admin.AdminName = (string)dr["AdminName"];
                    admin.PassWord = (string)dr["PassWord"];
                    admin.AdminDesc = (string)dr["AdminDesc"];

                    list.Add(admin);
                }
            }
            return list;
        }

        private static IList<Admin> GetAdminsBySql(string safeSql, params SqlParameter[] para)
        {
            List<Admin> list = new List<Admin>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
            {
                while (dr.Read())
                {
                    Admin admin = new Admin();

                    admin.AdminID = (int)dr["AdminID"];
                    admin.AdminName = (string)dr["AdminName"];
                    admin.PassWord = (string)dr["PassWord"];
                    admin.AdminDesc = (string)dr["AdminDesc"];

                    list.Add(admin);
                }
            }
            return list;
        }
    }
}
